<?php
namespace lemon\components;

use yii\web\View;
use yii\helpers\Html;

/**
 * 继承View,增加常用代码归置
 * @author shaodd
 *
 */
class WebView extends View
{
	public $menu="";
	public $actionColumn = "lemon\bootstrap4\ActionColumn";
	
	const POS_HDB = 6;
	
	protected $WHOLE_ROW=['options'=>['class'=>'form-group col-12 flex']];
	
	private $FLEXBOX = [
		1=>"col-lg-3 col-md-4 col-sm-6 col-xs-6",
		2=>"col-lg-6 col-md-8 col-sm-12 col-xs-12",
		3=>"col-12",
		4=>"col-6",
		5=>"col-3",
		6=>"col-6",
		12=>"col-12",
	];
	
	
	protected function viewItem($label, $value, $flex=1, $class=""){
		if($flex!=9){
			$class = $this->FLEXBOX[$flex];
		}
		$class .=' flex mt-2';
		return "<div class=\"{$class}\"><label>{$label}：</label><div>{$value}</div></div>";
	}
	protected function newLine(){
		return "<div class=\"col-12\"></div>";
	}
	protected function show($field, $labels, $values, $params='', $type='', $flex=1, $class=""){
		$label = $labels[$field]??'';
		$value = $values[$field]??'';
		
		if($type!='' && $value !='' && $value != null){
			$value = $this->enValue($type, $params, $field, $value);
		}
		if($flex!=9){
			$class = ' '.$this->FLEXBOX[$flex];
		}
		return "<div class=\"flex mt-2{$class}\"><label>{$label}：</label><div>{$value}</div></div>";
	}
	
	protected function section($field1, $field2, $label, $values, $params='', $type='', $flex=1, $class=""){
		$value1 = $values[$field1]??'';
		$value2 = $values[$field2]??'';
		
		if($type!='' && $value1 !='' && $value1 != null){
			$value1 = $this->enValue($type, $params, $field1, $value1);
		}
		if($type!='' && $value2 !='' && $value2 != null){
			$value2 = $this->enValue($type, $params, $field2, $value2);
		}
		if($flex!=9){
			$class = ' '.$this->FLEXBOX[$flex];
		}
		return "<div class=\"flex mt-2{$class}\"><label>{$label}：</label><div>{$value1} - {$value2}</div></div>";
	}
	
	protected function labelled($field, $label, $values, $params='', $type='', $flex=1, $class=""){
		$value = $values[$field]??'';
		
		if($type!='' && $value !='' && $value != null){
			$value = $this->enValue($type, $params, $field, $value);
		}
		if($flex!=9){
			$class = ' '.$this->FLEXBOX[$flex];
		}
		return "<div class=\"flex mt-2{$class}\"><label>{$label}：</label><div>{$value}</div></div>";
	}
	
	private function enValue($type, $params, $field, $value){
		if($type=="addon"){
			$value .= $params;
		}else if($type=="choose"){
			$choose = $params[$field]??[];
			$value = $choose[$value]??'';
		}elseif($type=="code"){
			$value = $params[$value]??'';
		}elseif($type=="float"){
			$value = floatval($value).$params;
		}elseif($type=="date" && intval($value) > 0){
			if($value==1){
				$value = '长期';
			}else{
				$value = date($params,intval($value));
			}
		}elseif($type=="money"){
			$value = Yii::$app->formatter->asDecimal($value??0, $place);
			$value = preg_replace('/(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))/', ',', $value);
			$value = $value.'元';
		}else if($type=="choose2"){
			if($params[0]==$value){
				$value = $values[$field.'_other']??'';
			}else{
				$choose = $params[1][$field]??[];
				$value = $choose[$value]??'';
			}
		}else if($type=="addon2"){
			if($value==0){
				$value = $value++.$params.'内';
			}else{
				$value = $value.$params;
			}
		}
		return $value;
	}
	
	protected function renderBodyEndHtml($ajaxMode){
		$content = parent::renderBodyEndHtml($ajaxMode);
		if (!empty($this->js[self::POS_HDB])) {
			$handlebar = $this->js[self::POS_HDB];
			$lines = [];
			foreach ($handlebar as $key=>$item){
				$lines[] = Html::script($item, ['id'=>$key, 'type'=>'text/x-handlebars-template']);
			}
			
			$content .= implode("\n", $lines);
		}
		return $content;
	}
	
}

